<template>
  <a-modal
    :title="modelTitle"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="角色定义名"
          help="英文唯一标识，如：super-admin"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-input
            v-decorator="['name', {rules: [{required: true, min: 1, message: '角色定义名不能为空'}]}]"
            placeholder="输入角色定义名" />
        </a-form-item>
        <a-form-item
          label="角色显示名"
          help="中文显示名，如：超级管理员"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-input
            v-decorator="['display_name', {rules: [{required: true, min: 1, message: '角色显示名不能为空'}]}]"
            placeholder="输入角色显示名" />
        </a-form-item>
        <a-form-item
          label="关联权限"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-tree-select
            :treeData="treePermData"
            v-decorator="['perm_ids']"
            treeCheckable
            :showCheckedStrategy="SHOW_ALL"
            searchPlaceholder="请选择权限" />
        </a-form-item>
        <a-form-item
          label="描述"
          help="权限说明"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-textarea
            v-decorator="['description']"
            :autoSize="{ minRows: 2, maxRows: 6 }"
            placeholder="输入权限描述" />
        </a-form-item>
        <a-form-item
          label="排序"
          help="数字越小越靠前"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-input-number
            v-decorator="['order_num', { initialValue: 0 }]"
            :min="1" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { TreeSelect } from 'ant-design-vue'
import { saveData } from '@/api/system/sysrole'
import { pick } from 'lodash'
const SHOW_ALL = TreeSelect.SHOW_ALL
export default {
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      },
      visible: false,
      confirmLoading: false,
      SHOW_ALL,
      form: this.$form.createForm(this),
      id: 0,
      parent_id: 0
    }
  },
  computed: {
    modelTitle: function () {
      if (this.id === 0) {
        if (this.parent_id === 0) {
          return '新建角色'
        } else {
          return '新建子角色'
        }
      } else {
        return '编辑角色'
      }
    }
  },
  props: {
    treePermData: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  methods: {
    add () {
      this.visible = true
      this.id = 0
      this.parentId = 0
      const { form: { resetFields } } = this
      this.$nextTick(() => {
        resetFields()
      })
    },
    addChild (record) {
      if (!record) {
        this.$message.error('请选中您想要添加子节点的角色')
        return false
      }
      this.visible = true
      this.id = 0
      this.parentId = record.id
      const { form: { resetFields } } = this
      this.$nextTick(() => {
        resetFields()
      })
    },
    edit (record) {
      this.visible = true
      this.id = record.id
      this.parentId = record.parent_id
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(pick(record, ['name', 'display_name', 'perm_ids', 'description', 'order_num']))
      })
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          saveData(Object.assign(values, { id: this.id, parent_id: this.parentId })).then(res => {
            this.confirmLoading = false
            if (res.code === 0) {
              this.visible = false
              this.$message.success(res.message)
              this.$emit('ok', values)
            } else {
              this.$message.error(res.message)
            }
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.visible = false
    }
  }
}
</script>
